<?php

namespace app\admin\controller;

use app\common\BaseJqGridController;
use app\common\RenderJson;
use app\model\SysUser;
use think\Request;

/**
 * 用户管理
 *
 * @author yyzh
 *        
 */
class SystemUser extends BaseJqGridController {
	function _getPage($pageSize) {
		$SysUser = new SysUser ();
		// 查询条件
		if ($this->request->has ( 'name' )) $SysUser->where ( 'name', 'LIKE', '%' . $this->request->param ( 'name' ) . '%' );
		if ($this->request->has ( 'account' )) $SysUser->where ( 'account', 'LIKE', '%' . $this->request->param ( 'account' ) . '%' );
		// 分页查询
		return $SysUser->paginate ();
	}
	function _addHandler($param) {
		return SysUser::create ( $param );
	}
	function _editHandler($param) {
		return SysUser::update ( $param );
	}
	function _delHandler($id) {
		return SysUser::destroy ( $id );
	}
	
	/**
	 * 重置密码
	 */
	public function restPasswrod($account = false) {
		$result = new RenderJson ();
		$account = $this->request->param ( 'account', false );
		$password = $this->request->param ( 'password' );
		$password_again = $this->request->param ( 'password_again' );
		if (! $account) {
			$result->msg ( '账号不存在' );
		} else if ($password != $password_again) {
			$result->msg ( '2次输入的密码不对' );
		} else {
			if (SysUser::update ( [ 
					'password' => $password 
			], [ 
					'account' => $account 
			] )) {
				$result->success ( true, '密码已重置' );
			} else {
				$result->msg ( '重置失败' );
			}
		}
		return $this->json ( $result );
	}
}
